package com.examples.calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

public class KeyboardListener implements KeyListener {
	
	Display display;
	Rectangle defaultRectangle;
	Rectangle newRectangle;
	Rectangle newRectangleToBeFormed;
	Rectangle oldRectangle;
	Rectangle oldRectangleToBeFocused;
	
	int rowIndex;
	int colIndex;

	public KeyboardListener(Display display, Rectangle rectangle) {
		this.display = display;
		this.defaultRectangle = rectangle;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void keyPressed(KeyEvent e) {
		
		Rectangle rectangle = e.display.getActiveShell().getClientArea();
		GC gc = new GC(display);
		defaultRectangle = RectangleHolder.getRectangle();
		
		 
		
		final int totalWidth = rectangle.width;
		final int totalHeight = rectangle.height;
		final int widthOfCell = (totalWidth - 5) / 7;
		final int heightOfCell = (totalHeight - 20) / 5;
		
		
		if(e.keyCode == SWT.ARROW_RIGHT){
				newRectangle = new Rectangle(defaultRectangle.x, defaultRectangle.y, defaultRectangle.width, defaultRectangle.height);
				defaultRectangle.x += widthOfCell;
				if(defaultRectangle.x + widthOfCell < totalWidth){
					newRectangleToBeFormed = display.map(display.getActiveShell(), null, defaultRectangle);
					
					gc.setXORMode(true);
					gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
					gc.fillRectangle(newRectangleToBeFormed);
					
					if(oldRectangle != null){
						oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
						gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
						gc.dispose();
					}
					oldRectangle = defaultRectangle;
					RectangleHolder.setRectangle(defaultRectangle);
				}
				else{
					defaultRectangle.x = 0;
					defaultRectangle.y += heightOfCell;
					if(defaultRectangle.y + heightOfCell < totalHeight){
						newRectangleToBeFormed = display.map(display.getActiveShell(), null, defaultRectangle);
						
						gc.setXORMode(true);
						gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
						gc.fillRectangle(newRectangleToBeFormed);
						
						if(oldRectangle != null){
							oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
							gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
							gc.dispose();
						}
						oldRectangle = defaultRectangle;
						RectangleHolder.setRectangle(defaultRectangle);
					}
					else{
						defaultRectangle.x += 6 * widthOfCell;
						defaultRectangle.y -= heightOfCell;
						RectangleHolder.setRectangle(defaultRectangle);
					}
				}
		}
		if(e.keyCode == SWT.ARROW_LEFT){
			newRectangle = new Rectangle(defaultRectangle.x, defaultRectangle.y, defaultRectangle.width, defaultRectangle.height);
			defaultRectangle.x -= widthOfCell;
			if(defaultRectangle.x >= 0){
				newRectangleToBeFormed = display.map(display.getActiveShell(), null, defaultRectangle);
				
				gc.setXORMode(true);
				gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
				gc.fillRectangle(newRectangleToBeFormed);
				
				if(oldRectangle != null){
					oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
					gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
					gc.dispose();
				}
				oldRectangle = defaultRectangle;
				RectangleHolder.setRectangle(defaultRectangle);
			}
			else{
				defaultRectangle.x += 7 * widthOfCell;
				defaultRectangle.y -= heightOfCell;
				if(defaultRectangle.y >= 20){
					newRectangleToBeFormed = display.map(display.getActiveShell(), null, defaultRectangle);
					
					gc.setXORMode(true);
					gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
					gc.fillRectangle(newRectangleToBeFormed);
					
					if(oldRectangle != null){
						oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
						gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
						gc.dispose();
					}
					oldRectangle = defaultRectangle;
					RectangleHolder.setRectangle(defaultRectangle);
				}
				else{
					defaultRectangle.x -= 6 * widthOfCell;
					defaultRectangle.y += heightOfCell;
					RectangleHolder.setRectangle(defaultRectangle);
				}
			}
		}
		if(e.keyCode == SWT.ARROW_DOWN){
			newRectangle = new Rectangle(defaultRectangle.x, defaultRectangle.y, defaultRectangle.width, defaultRectangle.height);
			defaultRectangle.y += heightOfCell;
			if(defaultRectangle.y + heightOfCell < totalHeight){
				newRectangleToBeFormed = display.map(display.getActiveShell(), null, defaultRectangle);
				
				gc.setXORMode(true);
				gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
				gc.fillRectangle(newRectangleToBeFormed);
				
				if(oldRectangle != null){
					oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
					gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
					gc.dispose();
				}
				oldRectangle = defaultRectangle;
				RectangleHolder.setRectangle(defaultRectangle);
			}
			else{
				defaultRectangle.y -= heightOfCell;
				RectangleHolder.setRectangle(defaultRectangle);
			}
		}
		if(e.keyCode == SWT.ARROW_UP){
			newRectangle = new Rectangle(defaultRectangle.x, defaultRectangle.y, defaultRectangle.width, defaultRectangle.height);
			defaultRectangle.y -= heightOfCell;
			if(defaultRectangle.y >= 20){
				newRectangleToBeFormed = display.map(display.getActiveShell(), null, defaultRectangle);
				
				gc.setXORMode(true);
				gc.setBackground(e.display.getSystemColor(SWT.COLOR_BLUE));
				gc.fillRectangle(newRectangleToBeFormed);
				
				if(oldRectangle != null){
					oldRectangleToBeFocused = e.display.map(e.display.getActiveShell(), null, newRectangle);
					gc.fillRectangle(oldRectangleToBeFocused.x + 1, oldRectangleToBeFocused.y + 1, oldRectangleToBeFocused.width-1,oldRectangleToBeFocused.height-1);
					gc.dispose();
				}
				oldRectangle = defaultRectangle;
				RectangleHolder.setRectangle(defaultRectangle);
			}
			else{
				defaultRectangle.y += heightOfCell;
				RectangleHolder.setRectangle(defaultRectangle);
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
	}

}
